/*
 * @Author: zhouwr 1551541860@qq.com
 * @Date: 2024-02-17 20:02:25
 * @LastEditors: zhouwr 1551541860@qq.com
 * @LastEditTime: 2024-02-21 21:30:33
 * @FilePath: /jeecgboot-vue3/src/views/activiti/procdef/ActProcessDef.api.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defHttp } from '/@/utils/http/axios'
import { useMessage } from '/@/hooks/web/useMessage'
import { uploadMyFile } from '/@/api/common/api'

const { createMessage } = useMessage()

enum Api {
  listPage = '/act/procdef/listPage',
  listFlowElement = '/act/procdef/listFlowElement',
  save = '/act/procdef/add',
  start = '/act/procinst/start',
  updateState = '/act/procdef/updateState',
  convertToModel = '/act/procdef/convertToModel',
  deleteOne = '/act/procdef/delete',
  getResourceSvg = '/act/procdef/resourceImage',
  getResourceXml = '/act/procdef/resourceXml',
}

/**
 * 列表接口
 * @param params
 */
export const listPage = (params) => defHttp.get({ url: Api.listPage, params })

export const startProcess = (params) => {
  return defHttp.get({ url: Api.start, params })
}
/**
 * 上传部署流程
 * @param params
 * @param isUpdate
 */
export const updateDeploy = (params, handleSuccess, handleError) => {
  const formData = new FormData()
  params.deployFileList.forEach((file) => {
    formData.append('file', file as any)
  })
  formData.append('category', params.category)
  return new Promise((resolve, reject) => {
    uploadMyFile(Api.save, formData).then((res: any) => {
      console.log('uploadMyFile', res)
      if (res && res.data) {
        if (res.data.success) {
          createMessage.info(res.data.message)
          resolve(1)
          handleSuccess()
        } else {
          createMessage.warning(res.data.message)
          reject(0)
          handleError()
        }
      } else {
        createMessage.error('上传部署流程失败！')
        reject(0)
        handleError()
      }
    })
  })
}

/**
 * 删除单个
 * @param params
 * @param handleSuccess
 */
export const deleteOne = async (params, handleSuccess) => {
  await defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true })
  handleSuccess()
}

/**
 * 激活和挂起
 * @param params
 * @param isUpdate
 */
export const updateState = async (params, handleSuccess) => {
  await defHttp.get({ url: Api.updateState, params })
  handleSuccess()
}

export const convertToModel = async (params: any) => {
  const res = await defHttp.get({ url: Api.convertToModel, params })
  console.log(res)
  createMessage.success('成功转换为模型，模型id=' + res.id)
}

export const getActResourceSvg = (params: any) => {
  return defHttp.get({ url: Api.getResourceSvg, params }, { isTransformResponse: false })
}

export const getActResourceXml = (params: any) => {
  return defHttp.get({ url: Api.getResourceXml, params }, { isTransformResponse: false })
}
